package com.geper.pattern.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lsy
 * @date: 2020-03-09 20:21
 * @description: TreeNode 树枝节点
 */
public class TreeNode extends RootNode{

    private List<RootNode> tree;

    private int level;

    public TreeNode(String name,int level) {
        super(name);
        tree = new ArrayList<RootNode>();
        this.level = level;
    }

    public void addChild(RootNode node){
        this.tree.add(node);
    }

    @Override
    public String show(int level) {
        StringBuilder sb = new StringBuilder();
        if(tree == null || tree.size() == 0){
            return sb.toString();
        }
        sb.append("+").append(name);
        //遍历下级节点
        for (RootNode node : tree) {
            sb.append("\n");
            for (int i = 0; i < level; i++) {
                sb.append("  ");
            }
            //名称并换行
            sb.append(node.show(level + 1));
        }

        return sb.toString();
    }
}
